package com.orangelife.mobile.common.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.util.StringUtil;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 公用的提示页
 * <p/>
 * Created by YangWei on 2015/11/13.
 */
public class BaseCompleteActivity extends FragmentActivity {

    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.ivSuccess)
    ImageView ivSuccess;
    @Bind(R.id.tvMessageTitle)
    TextView tvMessageTitle;
    @Bind(R.id.tvMessageInfo)
    TextView tvMessageInfo;
    @Bind(R.id.tvMessageTimer)
    TextView tvMessageTimer;
    @Bind(R.id.button)
    Button button;
    private int timeSendSms = 5;
    private Timer timer;
    private String timeInfo = "后返回首页";

    private CallBackEvent callBackEvent;
    private String className;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_complete);
        ButterKnife.bind(this);
        getIntentInfo();
    }

    private void getIntentInfo() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            final String className = bundle.getString("className");
            String btnMsg = bundle.getString("btnMsg");
            String title = bundle.getString("title");
            String tip = bundle.getString("tip");
            setBtnSubmit(btnMsg);
            setTvTitle(title);
            setTvMessageInfo(tip);
            setCallBackEvent(new CallBackEvent() {
                @Override
                public void setBtnSubmit() {

                }

                @Override
                public void JumpToActivity() {
                    if (!TextUtils.isEmpty(className)) {
                        Intent intent = new Intent();
                        intent.setClassName(BaseCompleteActivity.this, className);
                        startActivity(intent);
                    } else {
                        finish();
                    }

                }
            });
        }
    }

    /**
     * 必须实现此方法，因为xml的onClick有定义此方法，如无实现此方法，则会报NoSuchMethod的错误
     */
    public void onBackClick(View v) {
        finish();
    }

    @OnClick(R.id.button)
    public void onClick() {
        ToastHelper.getInstance()._toast("成功");
        callBackEvent.JumpToActivity();
    }

    /**
     * 设置提示图案
     *
     * @param drawable
     */
    public void setIvSuccess(Drawable drawable) {
        ivSuccess.setImageDrawable(drawable);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTvTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 设置显示内容，为空为隐藏
     *
     * @param btnSubmit    按钮
     * @param timeInfo     读秒信息
     * @param messageTitle 提示标题
     * @param messageInfo  提示内容
     */
    public void setMessage(String btnSubmit, String timeInfo, String messageTitle, String messageInfo) {
        setBtnSubmit(btnSubmit);
        setTimeInfo(timeInfo);
        setTvMessageTitle(messageTitle);
        setTvMessageInfo(messageInfo);
    }

    /**
     * 设置按钮提示，message为空则设置为隐藏
     *
     * @param message
     */
    public void setBtnSubmit(String message) {
        if (!StringUtil.isBlank(message)) {
            button.setVisibility(View.VISIBLE);
            button.setText(message);
        } else {
            button.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置提示标题，message为空则设置为隐藏
     *
     * @param message
     */
    public void setTvMessageTitle(String message) {
        if (!StringUtil.isBlank(message)) {
            tvMessageTitle.setVisibility(View.VISIBLE);
            tvMessageTitle.setText(message);
        } else {
            tvMessageTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 设置提示内容，message为空则设置为隐藏
     *
     * @param message
     */
    public void setTvMessageInfo(String message) {
        if (!StringUtil.isBlank(message)) {
            tvMessageInfo.setVisibility(View.VISIBLE);
            tvMessageInfo.setText(message);
        } else {
            tvMessageInfo.setVisibility(View.INVISIBLE);
        }
    }


    /**
     * 设置读秒提示，message为空则设置为隐藏
     *
     * @param message
     */
    public void setTimeInfo(String message) {
        if (!StringUtil.isBlank(message)) {
            this.timeInfo = message;
            tvMessageTimer.setVisibility(View.VISIBLE);
            countDownTime();
        } else {
            tvMessageTimer.setVisibility(View.INVISIBLE);
        }

    }

    /**
     * 设置回调对象
     *
     * @param callBackEvent
     */
    public void setCallBackEvent(CallBackEvent callBackEvent) {
        this.callBackEvent = callBackEvent;
    }

    public interface CallBackEvent {
        /**
         * 设置按钮事件
         */
        void setBtnSubmit();

        /**
         * 设置读秒结束时的跳转
         */
        void JumpToActivity();
    }

    /**
     * 重置验证码时间
     */
    private void countDownTime() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.obtainMessage(Constant.WHAT_COUNT_DOWN, timeSendSms--)
                        .sendToTarget();
            }
        }, 0, 1000);
    }

    private TimeHandler mHandler = new TimeHandler(this);

    private static class TimeHandler extends Handler {
        private final WeakReference<BaseCompleteActivity> weakReference;

        public TimeHandler(BaseCompleteActivity act) {
            weakReference = new WeakReference<>(act);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.WHAT_COUNT_DOWN:
                    BaseCompleteActivity act = weakReference.get();
                    if (act == null) {
                        return;
                    }
                    if (act.timeSendSms >= 0) {
                        act.tvMessageTimer.setText(act.timeSendSms + act.timeInfo);
                    } else {
                        act.timer.cancel();
                        if (act.callBackEvent != null) {
                            act.callBackEvent.JumpToActivity();
                        }
                        CurryApplication.getInstance().finishActivity(act);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}
